package cn.wolfcode.crud.web.controller;

import cn.wolfcode.crud.domain.Employee;
import cn.wolfcode.crud.domain.Permission;
import cn.wolfcode.crud.domain.Role;
import cn.wolfcode.crud.qo.PageResult;
import cn.wolfcode.crud.qo.QueryObject;
import cn.wolfcode.crud.service.IPermissionService;
import cn.wolfcode.crud.service.IRoleService;
import cn.wolfcode.crud.util.RequiredPermission;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

@Controller
@RequestMapping("/role")
public class RoleController {
    @Autowired
    private IRoleService roleService;
    @Autowired
    private IPermissionService permissionService;

    @RequiredPermission("角色页面")
    @RequestMapping("/list")
    public String list(Model model, @ModelAttribute("qo")QueryObject qo){
        model.addAttribute("pageInfo",roleService.query(qo));
        return "role/list";
    }

    @RequiredPermission("角色编辑页面")
    @RequestMapping("/input")
    public String input(Model model, Long id){
        List<Permission> permissions = permissionService.listAll();
        model.addAttribute("permissions",permissions);
        if(id!=null){
          Role role = roleService.getById(id);
          model.addAttribute("role",role);
       }
        return "role/input";
    }

    @RequiredPermission("角色新增/修改")
    @RequestMapping("/saveOrUpdate")
    public String saveOrUpdate(Role role, Long[] ids){

        if (role.getId() != null) {
            roleService.update(role,ids);
        }else {
            roleService.save(role,ids);
        }

        return "redirect:/role/list.do";
    }

    @RequiredPermission("角色删除")
    @RequestMapping("/delete")
    public String delete(Long id){
        if (id!=null) {
            roleService.delete(id);
        }
        return  "redirect:/role/list.do";
    }
}
